package la.iok.aone.monitor.youtube.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import la.iok.aone.monitor.youtube.model.YoutubeLink;
import la.iok.aone.monitor.youtube.service.YoutubeLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="/monitor", tags="Youtube链接管理")
@RestController
@RequestMapping("/monitor")
public class MonitorController {
    @Autowired
    private YoutubeLinkService youtubeLinkService;

    @ApiOperation(value = "获取Youtube监控链接", notes = "以轮询的方式获取Youtube监控链接。\n" +
            "   记录游标，每次从游标处获取指定数量的链接。\n" +
            "   新加入的链接只能等待下一次游标经过时才会被读取监控。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "需要获取的Youbute链接数量", dataType = "int", defaultValue = "10", required = false, paramType = "query"),
            @ApiImplicitParam(name = "category", value = "视频分类", dataType = "String", required = false, paramType = "query")
    })
    @GetMapping("youtube")
    @JsonView(YoutubeLink.MonitorView.class)
    public List<YoutubeLink> polling(@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                     @RequestParam(value = "category", required = false) String category
            ) {
        return this.youtubeLinkService.selectByPolling(category, pageSize);
    }

    @ApiOperation(value = "获取Youtube监控链接", notes = "以平滑轮询的方式获取Youtube监控链接。\n" +
            "   平滑的轮询，新加入的链接会逐渐追上较早加入链接的监控次数。")
    @ApiImplicitParam(name = "pageSize", value = "需要获取的Youbute链接数量", dataType = "int", defaultValue = "10", required = false, paramType = "query")
    @GetMapping("youtube/smooth")
    @JsonView(YoutubeLink.MonitorView.class)
    public List<YoutubeLink> pollingSmooth(@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return this.youtubeLinkService.selectBySmoothPolling(pageSize);
    }

    @ApiOperation(value = "下线Youtube链接", notes = "提供给多画面监控调用，如果获取到的链接视频播放有问题，则调用该接口下线链接。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Youtube链接编号", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "reason", value = "下线原因", dataType = "String", required = true, paramType = "query")
    })
    @PutMapping("youtube/offline")
    public Map<String, Object> offlineYoutubeLink(int id, String reason) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            this.youtubeLinkService.offlineYoutubeLink(id, reason);
            result.put("result", true);
        } catch (Exception e) {
            result.put("result", false);
        }
        return result;
    }
}
